// timer_controller.dart
import 'dart:async';

class TimerController {
  final int timeLimit;
  late int _elapsedTime;
  late Timer _timer;

  // 回调函数定义
  Function(int)? onTick;
  Function? onTimeout;

  TimerController(this.timeLimit);

  void start() {
    _elapsedTime = 0;
    _timer = Timer.periodic(const Duration(seconds: 1), (timer) {
      _elapsedTime++;
      onTick?.call(_elapsedTime);
      if (_elapsedTime >= timeLimit) {
        stop();
        onTimeout?.call();
      }
    });
  }

  void stop() {
    if (_timer.isActive) {
      _timer.cancel();
    }
  }

  void reset() {
    stop();
    _elapsedTime = 0;
    onTick?.call(_elapsedTime);
  }

  void dispose() {
    stop();
  }
}